package ael.imarkket.Library;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;

public class ConnectionChecking {

	public ProgressDialog				dialogProcess	= null;

	private static ConnectionChecking	instance		= new ConnectionChecking();
	static Context						context;

	public static ConnectionChecking getInstance(Context ctx) {
		context = ctx;
		return instance;
	}

	public boolean CheckWIFI() {
		ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

		android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		return wifi.isConnected();
	}

	public boolean CheckMOBILE() {
		ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

		android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

		return mobile.isConnected();
	}

	public boolean CheckInternetURL(String str_URL) {

		Boolean ret = false;

		try {
			URL url = new URL(str_URL.toString());
			HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
			urlc.setConnectTimeout(3000); // This is time limit if the
											// connection time limit
			urlc.connect();
			if (urlc.getResponseCode() == 200) {
				ret = true;
			} else {
				ret = false;
			}
		} catch (MalformedURLException e) {
			Logging.getInstance().log(Logging.LOG_ERROR, "CheckInternetURL MalformedURLException: " + e.toString());
			ret = false;
		} catch (IOException e) {
			Logging.getInstance().log(Logging.LOG_ERROR, "CheckInternetURL IOException: " + e.toString());
			ret = false;
		}
		return ret;
	}
}